﻿using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PerfTestLib.Result
{
    public class MongoTestSpecResultRepository : ITestSpecResultRepository
    {
        private MongoClient Client { get; set; }
        private MongoDatabase Database { get; set; }


        public MongoTestSpecResultRepository(string connectionString)
        {
            // DB address
            MongoUrl mongoUrl = MongoUrl.Create(connectionString);

            if (string.IsNullOrWhiteSpace(mongoUrl.DatabaseName))
            {
                throw new ArgumentException("No database specified in the connection string!");
            }

            // Mongo DB client
            this.Client = new MongoClient(mongoUrl);

            // Mongo DB server
            MongoServer server = this.Client.GetServer();

            // Mongo DB
            this.Database = server.GetDatabase(mongoUrl.DatabaseName);
        }

        public void Insert(TestSpecResult result, string collectionName = "TestSpecResult")
        {
            var col = this.Database.GetCollection<TestSpecResult>(collectionName);
            col.Insert(result);
        }
    }
}
